package dbbaas;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
 
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
 
 
public class FileHelper {
    /**
     * 统计文件（隐藏）
     */
    private final static String STATISTIC_FILE = ".cnt";
     
    private final static int MAX_FILE_CNT_PER_FOLDER=30000;
    /**
     * 保存文件
     * 
     * @param data
     * @param savePath
     * @param fileName
     * @return
     */
//    public static String saveFile(byte[] data, String savePath,String fileName) {
//        String retFileName="";
//        if (!StringUtils.endsWith(savePath, "/")) {
//            savePath += "/";
//        }
//        try {
//             
//            FileOperate fileOperate = new FileOperate(savePath + STATISTIC_FILE);
//            int iFiles = ConvertUtil.ConvertString2Integer(fileOperate
//                    .readData(CNT_FILE_STRUCT.FILE_CNT_KEY));
//            int iFolder = ConvertUtil.ConvertString2Integer(fileOperate
//                    .readData(CNT_FILE_STRUCT.FOLDER_CNT_KEY));
//            if(iFolder>MAX_FILE_CNT_PER_FOLDER){
//                throw new Exception("文件目录已经满了，无法再写");
//            }
//            int iFolderName=iFiles/MAX_FILE_CNT_PER_FOLDER+1;
//             
//            retFileName=iFolderName+"/"+fileName;
//             
//            FileUtils.writeByteArrayToFile(new File(savePath+retFileName), data);
//            //保存文件后处理
//            iFiles+=1;
//            fileOperate.writeData(CNT_FILE_STRUCT.FILE_CNT_KEY, iFiles+"");
//            fileOperate.writeData(CNT_FILE_STRUCT.FOLDER_CNT_KEY, iFolderName+"");
//             
//        } catch (NumberFormatException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return retFileName;
//    }
// 
//    /**
//     * 删除文件
//     * @param filePath
//     * @param fileName
//     * @return
//     */
//    public static boolean delFile(String filePath,String fileName){
//        if (!StringUtils.endsWith(filePath, "/")) {
//            filePath += "/";
//        }
//         
//        FileOperate fileOperate = new FileOperate(filePath + STATISTIC_FILE);
//        File delFile=new File(filePath+fileName);
//        if(delFile.exists()){
//            delFile.delete();
//            int iFile=ConvertUtil.ConvertString2Integer(fileOperate.readData(CNT_FILE_STRUCT.FILE_CNT_KEY));
//            fileOperate.writeData(CNT_FILE_STRUCT.FILE_CNT_KEY, ""+(iFile-1));
//        }
//        return true;
//    }
//     
//    class CNT_FILE_STRUCT {
//        public static final String FILE_CNT_KEY = "file_count";
//        public static final String FOLDER_CNT_KEY = "folder_count";
//    }
}
class FileOperate {
    /**
     * 指定property文件
     */
    private String PROPERTY_FILE;
 
    public FileOperate(String property_file) {
        super();
        PROPERTY_FILE = property_file;
    }
 
    /**
     * 根据Key 读取Value
     * 
     * @param key
     * @return
     */
    public String readData(String key) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(
                    PROPERTY_FILE));
            props.load(in);
            in.close();
            String value = props.getProperty(key);
            return value;
        } catch (Exception e) {
            return null;
        }
    }
 
    /**
     * 修改或添加键值对 如果key存在，修改 反之，添加。
     * 
     * @param key
     * @param value
     */
    public void writeData(String key, String value) {
        Properties prop = new Properties();
        try {
            java.io.File file = new java.io.File(PROPERTY_FILE);
            if (!file.exists())
                file.createNewFile();
            InputStream fis = new FileInputStream(file);
            prop.load(fis);
            fis.close();// 一定要在修改值之前关闭fis
            OutputStream fos = new FileOutputStream(PROPERTY_FILE);
            prop.setProperty(key, value);
            prop.store(fos, "Update '" + key + "' value");
            fos.close();
        } catch (IOException e) {
            System.err.println("Visit " + PROPERTY_FILE + " for updating "
                    + value + " value error");
        }
    }
}
